#include "MainThread.h"

MainThread::MainThread(void)
{
}

MainThread::~MainThread(void)
{
}

bool  MainThread::reachedVertex(CarManager* manager,GraphDrawer* drawer){
	float vX = manager->getVNext()->getX();
	float vY = manager->getVNext()->getY();
	vX = drawer->fixXPosition(vX);
	vY = drawer->fixYPosition(vY);
	return manager->carAtVertex(vX,vY);
}



void MainThread::run(Map* map, List* vertexListPath, string idEdgeFrom, int addFrom, string idEdgeTo, int addTo, List* objects, int option) {

	// Graph Drawer
	GraphDrawer* drawer = new GraphDrawer();
	SDL_Surface* screen = drawer->drawGraph(map);

	// Viewport Configuration
	T_Resolution viewportSize;
	viewportSize.width = Utils::stringToInt(Utils::getConfigurationValue("ViewportX"));
	viewportSize.height = Utils::stringToInt(Utils::getConfigurationValue("ViewportY"));

	T_Resolution screenResolution;
	screenResolution.width = Utils::stringToInt(Utils::getConfigurationValue("ResolutionX"));
	screenResolution.height = Utils::stringToInt(Utils::getConfigurationValue("ResolutionY"));
	// Viewport
	Viewport viewport(screen, viewportSize, screenResolution);
	viewport.draw();

	// Path Drawer (for the first time)
	GraphManager* graphManager = new GraphManager(map->getGraph());

	PathManager* pathManager = new PathManager(map, graphManager, idEdgeFrom, addFrom, idEdgeTo, addTo, objects, option);
	PathDrawer* pathDrawer = new PathDrawer(drawer);
	viewport.addSurfaceToRenderList(pathDrawer->drawPath(map, pathManager->getPathToDraw(idEdgeFrom, addFrom), idEdgeFrom, addFrom, pathManager->getIdEdgeTo(), pathManager->getAddTo()), "path", 0, 0, VIEWPORT_ATTACHED);
	
	SDL_Surface* carScreen = viewport.getWindowScreen();	

	CarManager* manager = new CarManager(carScreen, &viewport);
	List* lookupVertexList = vertexListPath;
	Vertex* additionalVertex = NULL;
	
	float carX;
	float carY;
	manager->calcInitPosition(carX,carY,map,idEdgeFrom,addFrom,idEdgeTo,drawer);
	manager->drawPanel(true, vertexListPath,map->getGraph()->getVertex((map->getGraph()->getEdge(idEdgeTo))->getIdTo())/*, NULL*/);
	manager->drawCar(carX,carY,true);	

	int done=0;
	SDL_Event event;

	// Main Thread

	while(done == 0)
	{
		Uint32 past = SDL_GetTicks();
		while ( SDL_PollEvent(&event) ){
			
			if ( event.type == SDL_QUIT ){
				done = 1;
			} else {
				if ( event.type == SDL_KEYDOWN ) {
					if (event.key.keysym.sym == SDLK_a ||
						event.key.keysym.sym == SDLK_d ||
						event.key.keysym.sym == SDLK_w ||
						event.key.keysym.sym == SDLK_s) {
							viewport.refreshMovs();

					} else if (event.key.keysym.sym == SDLK_UP ||
						event.key.keysym.sym == SDLK_RIGHT ||
						event.key.keysym.sym == SDLK_LEFT) {
							manager->turn(event.key.keysym.sym);

					} else if (event.key.keysym.sym == SDLK_z){
						manager->speedUp();

					} else if (event.key.keysym.sym == SDLK_x){
						manager->speedDown();
					}
				}

			}
		}
		viewport.render();
		bool reached = reachedVertex(manager,drawer);
		if (reached){
			graphManager->removePhantomEdge();
			manager->updateCarPosition(map, graphManager);
			List* pathToDraw = pathManager->getPathToDraw(manager->getActualEdge()->getId(), manager->getActualEdge()->getInitAddress());
			lookupVertexList = pathToDraw;
			/*string idFrom = pathManager->getIdEdgeFrom();			
			if (lookupVertexList->empty()){
				//additionalVertex = map->getGraph()->getVertex(map->getGraph()->getEdge(idFrom)->getIdTo());
			}
			*/
			viewport.addSurfaceToRenderList(pathDrawer->drawPath(map, pathToDraw, manager->getActualEdge()->getId(), manager->getActualEdge()->getInitAddress(), pathManager->getIdEdgeTo(), pathManager->getAddTo()), "path", 0, 0, VIEWPORT_ATTACHED);
		}
		manager->timePasses();	
		manager->drawAll(lookupVertexList,map->getGraph()->getVertex((map->getGraph()->getEdge(idEdgeTo))->getIdTo())/*, additionalVertex */);
		
		int delay = manager->getSleep();
		//SDL_Delay(delay);
		Uint32 now = SDL_GetTicks();
		delay = delay - (now - past);
		if (delay > 0){
			SDL_Delay(delay);
		}
	}
	delete drawer;
	delete pathDrawer;
	delete manager;
}
